package com.mixpanel.example.hello.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.mixpanel.android.mpmetrics.MPConfig;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import com.mixpanel.android.util.L;
import com.mixpanel.android.util.SP;
import com.mixpanel.example.hello.App;
import com.mixpanel.example.hello.R;
import com.tencent.stat.StatConfig;
import com.tencent.stat.StatReportStrategy;

import org.json.JSONArray;

/**
 * 启动界面
 */
public class LaunchActivity extends Activity {

    public static final String SP_URL = "url";
    private EditText etServerURLByLaunch = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        initUI();
        initTencentSDK();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    MixpanelAPI.getInstance(LaunchActivity.this, App.MIXPANEL_API_TOKEN).track(
                            "LaunchActivity..." + System.currentTimeMillis());
                }
            }
        }).start();
    }

    /**
     * 初始化腾讯统计平台
     */
    private void initTencentSDK() {
        // 打开debug开关，可查看mta上报日志或错误
        // 发布时，请务必要删除本行或设为false
        StatConfig.setDebugEnable(true);
        com.tencent.stat.StatService.trackCustomEndEvent(this, "onCreate_initTencentSDK", "");
        // 根据情况，决定是否开启MTA对app未处理异常的捕获
        StatConfig.setAutoExceptionCaught(true);
        // 选择默认的上报策略
        StatConfig.setStatSendStrategy(StatReportStrategy.APP_LAUNCH);
        StatConfig.setSessionTimoutMillis(10);
    }

    @Override
    protected void onResume() {
        super.onResume();

        /******************* 腾讯统计Begin *********************/
        com.tencent.stat.StatService.onResume(this);
        /******************* 腾讯统计End *********************/

        JSONArray ar = new JSONArray();
        ar.put("a");
        ar.put("b");
        JSONArray ara = new JSONArray();
        ara.put("c");
        ara.put("d");
        L.e("ar:" + ar);
        L.e("ara:" + ara);
        L.e(ara.put(ar));
    }

    @Override
    protected void onPause() {
        super.onPause();

        /******************* 腾讯统计Begin *********************/
        com.tencent.stat.StatService.onPause(this);
        /******************* 腾讯统计End *********************/
    }

    private void initUI() {
        //url 编辑框
        etServerURLByLaunch = (EditText) this.findViewById(R.id.etServerURLByLaunch);
        //状态展示
        String spURL = (String) SP.getParam(SP_URL, "");
        if (!TextUtils.isEmpty(spURL)) {
            etServerURLByLaunch.setText(spURL);
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.startShowingByLaunch:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.btnSaveServerURLByLaunch:
                Editable et = etServerURLByLaunch.getText();
                if (et != null) {
                    String url = et.toString();
                    if (!TextUtils.isEmpty(url)) {
                        SP.setParam(SP_URL, url);
                        MPConfig.getInstance(this).setEditorUrl(url);
                    }
                }
                break;
            case R.id.btnGoBro:
                startActivity(new Intent(this, BrotestActivity.class));
                break;
            case R.id.btnPramChecker:
                startActivity(new Intent(this, SyncEcPageActivity.class));
                break;
            default:
                break;
        }
    }
}
